package team.tunan.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import team.tunan.common.LayUIResult;
import team.tunan.common.Result;
import team.tunan.entity.Game;
import team.tunan.service.IGameService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Controller
@RequestMapping("/game")
public class GameController {
    @Autowired
    private IGameService gameService;


    @RequestMapping("/doAdd")
    @ResponseBody
    public Result doAdd(Game game){
        int i = gameService.doAdd(game);
//        int i =1;
        return new Result(i>0?"成功":"失败",null,i>0);
    }

    /**
     * 搜索框
     * @return
     */
    @RequestMapping("/gameInfo")
    @ResponseBody
    public LayUIResult getplayinfo(Integer isAll){
        List<Map<String, Object>> playerInfo = gameService.getGameInfo(isAll);
        int size = playerInfo.size();
        return new LayUIResult(0,null,size,playerInfo);
    }

    /**
     * 查询所有比赛回显主页
     * @param model
     * @return
     */
    @RequestMapping("/data")
    @ResponseBody
    public LayUIResult data(Model model){
        List<Map<String,Object>> matchInfo = gameService.getGameInfoTwo();
        long count = matchInfo.stream().count();
        if (matchInfo==null){

            return new LayUIResult(1,"失败",0,null);
        }
        return new LayUIResult(0,"成功",count,matchInfo);
    }


    /**
     * 返回数据进行回显数据
     * @param mid
     * @param model
     * @return
     */
    @RequestMapping("/edit/{mid}")
    public String e(@PathVariable Integer mid, Model model){
        Game game = gameService.getGame(mid);
        model.addAttribute("game",game);
        return "match/editGame";
    }


    @RequestMapping("/update")
    @ResponseBody
    public Result update( Game game){
        Map<String, Object> map = gameService.updateGame(game);
        boolean flag = (boolean)map.get("flag");
        Integer status = (Integer)map.get("status");
        String msg = null;
        if (status == 0){
            msg = "比赛还有比赛场次没结束不能结束大比赛";
        }else if(status == 1){
            msg = "比赛还没开始不能直接结束";
        }else if(status ==2){
            msg = "比赛已经结束";
        }else if(status == 3){
            msg = "比赛成功开启";
        }else if(status == 4){
            msg = "比赛开启失败";
        }
        return new Result(msg,null,true);
    }
}

